/*
 * tree widget
 */
Qlsolution.widgets.tree = {

	/*
	 * load/refresh tree
	 */
	load: function(settings){
		//show loading
		$(settings.treeCls + ' .elementHeader .loading').show();
		//set default node
		if(!settings.node){
			settings.node = 0;
		}
		var paramsData = new Object();
			paramsData['id_node'] = settings.node;
		$.ajax({
			type: 'post',
			data: 'params=' + $.toJSON(paramsData),
			url: Qlsolution.baseUrl + settings.storeUrl,
			success: function(msg){
				$(settings.treeCls + ' .elementHeader .loading').hide();
				Qlsolution.widgets.tree.setContent(settings, msg);
				Qlsolution.widgets.tree.handleTreeElements(settings);
				//Qlsolution.widgets.tree.setSortable(settings);
			}
		});
	},

	/*
	 * set content tree
	 */
	setContent: function(settings, msg){
		var treeData = $.parseJSON(msg);
		if(settings.node == 0){
			$(settings.treeCls + ' .elementContent').html('<ul></ul>');
			for (var i = 0; i < treeData.length; i++){
				if(treeData[i].leaf == 0){
					$(settings.treeCls + ' .elementContent ul').append('<li class="connectedSortable"><span class="group plus cp" id="' + settings.idPrefix + treeData[i].id + '">' + treeData[i].text + '</span><div class="children"></div></li>');
				}
				if(treeData[i].leaf == 1){
					$(settings.treeCls + ' .elementContent ul').append('<li class="connectedSortable"><span class="leaf cp" id="' + settings.idPrefix + treeData[i].id + '">' + treeData[i].text + '</span></li>');
				}
			}
		}
		else{
			$(settings.treeCls + ' .elementContent span#' + settings.treeCls.substring(1) + settings.node).parent().find('.children').html('<ul></ul>');
			for (var i = 0; i < treeData.length; i++) {
				if (treeData[i].leaf == 0) {
					$(settings.treeCls + ' .elementContent span#' + settings.treeCls.substring(1) + settings.node).parent().find('.children ul').append('<li class="connectedSortable"><span class="group cp" id="' + settings.idPrefix + treeData[i].id + '">' + treeData[i].text + '</span><div class="children"></div></li>');
				}
				if (treeData[i].leaf == 1) {
					$(settings.treeCls + ' .elementContent span#' + settings.treeCls.substring(1) + settings.node).parent().find('.children ul').append('<li class="connectedSortable"><span class="leaf cp" id="' + settings.idPrefix + treeData[i].id + '">' + treeData[i].text + '</li>');
				}
			}

		}
	},

	/*
	 * handle header icons
	 */
	handleHeaderIcons: function(settings){
		var btns = $(settings.treeCls + ' .elementMenu');
		//refresh
		btns.find('.arrowRefreshSmall').click(function(){
			settings.node = 0;
			Qlsolution.widgets.tree.load(settings);
		});
	},

	/*
	 * handle elements
	 */
	handleTreeElements: function(settings){
		var idElement,
			$selector = $(settings.treeCls + ' .elementContent ul li');
		//group
		$selector.find('span.group').unbind('click');
		$selector.find('span.group').click(function(){
			if ($(this).hasClass('plus')) {
				idElement = $(this).attr('id');
				idElement = idElement.substring(settings.treeCls.length - 1);
				settings.node = idElement;
				Qlsolution.widgets.tree.load(settings);
				$(this).removeClass('plus').addClass('minus');
			}
			else{
				$(this).removeClass('minus').addClass('plus');
				$(this).parent().find('.children ul').remove();
			}
		});
		//leaf
		$selector.find('span.leaf').unbind('click');
		$selector.find('span.leaf').click(function(){
			$('.alert').remove();
			$selector.find('span.leaf.active').removeClass('active');
			$(this).addClass('active');
			idElement = $(this).attr('id');
			idElement = idElement.substring(settings.treeCls.length - 1);
			settings.node = idElement;
			if((Qlsolution.controllers[settings.controllerName]) && (Qlsolution.controllers[settings.controllerName].handleTreeLeaf)){
				Qlsolution.controllers[settings.controllerName].handleTreeLeaf(idElement); //implementing in controller
			}
		});
	},

	/*
	 * set dragable columns
	 */
	setSortable: function(settings){
		var group = $(settings.treeCls + ' .elementContent ul li span.group').parent();
		var leaf = $(settings.treeCls + ' .elementContent ul li span.leaf').parent();
		$(leaf, group).sortable({
			connectWith: '.connectedSortable',
			placeholder: 'widgetTreePlaceholder'
		}).disableSelection();
	},

	start: function(settings){
		Qlsolution.widgets.tree.load(settings);
		Qlsolution.widgets.tree.handleHeaderIcons(settings);
	}

};